package ms.jasim.console;

import java.awt.BorderLayout;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ms.jacrim.framework.ActiveSolution;
import ms.jacrim.pddl.PddlSolution;
import ms.spm.IAppContext;

public class ActiveSolutionFrame extends JasimDockablePanel {
	private static final long serialVersionUID = 1L;
	private JTextArea textArea;
	
	public ActiveSolutionFrame(IAppContext context) {
		super(context);
		createContent();
		
		setText("Active Solution");
		refresh();
	}

	@Override
	protected void setupContext() {
		super.setupContext();
		svcJasim.addListener(new JasimClientListener() {

			@Override
			public void notifyListener() {
				refresh();
			}

		});
	}
	
	private void createContent() {
		setLayout(new BorderLayout());

		textArea = new JTextArea();

		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(scrollPane, BorderLayout.CENTER);
	}

	void refresh() {
		ActiveSolution sol = svcJasim.getActiveSolution();
		if (sol != null) {
			StringBuilder s = new StringBuilder();
			for (int i = 0; i < sol.ActionCount(); i++) {
				PddlSolution.Action act = sol.getAction(i);
				s.append(act.toString());
				ActiveSolution.ActionInfo info = sol.findActionInfo(act);
				if (info != null)
					s.append(info.toString());
				s.append('\n');
			}
			textArea.setText(s.toString());
		}
		else
			textArea.setText("");
	}
}
